package com.gem.controller.mobile;

import com.alibaba.druid.util.StringUtils;
import com.gem.domain.CampusVisit;
import com.gem.domain.User;
import com.gem.service.CampusVisitService;
import com.gem.service.UserService;
import com.gem.util.ExcelUtil;
import com.gem.util.MessageUtil;
import com.gem.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import static com.gem.util.ExcelUtil.setHSSFValidation;

/**
 * @Author: 玲
 * @Description:
 * @create 2019-10-21 9:30
 * @Modified By:
 */
@Controller
@SuppressWarnings("all")
public class MobileCampusVisitController {

    @Autowired
    private CampusVisitService service;

    @Autowired
    private UserService userService;

    @RequestMapping("/mcampusVisit/findAll")
    public String findAll(Model model, HttpSession session,
                          @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                          @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            User user = (User) session.getAttribute("user");

            List<User> users = userService.findMmembers();
            model.addAttribute("users", users);
            CampusVisit campusVisit = new CampusVisit();
            campusVisit.setPersonname(user.getPersonname());
            PageInfo<CampusVisit> pageBeans = null;
            if (user.getAuthority() == 1) {
                pageBeans = service.findAll(currentPage, pageSize);
            } else {
                pageBeans = service.search(campusVisit, currentPage, pageSize);
            }
            if (pageBeans != null && pageBeans.getList().size() > 0) {
                model.addAttribute("pageBean", pageBeans);
                List<Integer> pageValue = PageUtil.getPageValue(pageBeans.getPages(), currentPage);
                model.addAttribute("pageValue", pageValue);
            } else {
                System.out.println();
                model.addAttribute("info", "没有数据请先添加！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/", "服务器异常！请联系管理员！", model);
            return "mobile_message";
        }
        return "mobile_campus/visit";
    }

    @RequestMapping("/mcampusVisit/saveUI")
    public String saveUI(Model model) {
        try {
            List<User> users = userService.findMmembers();
            model.addAttribute("users", users);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/mcampusVisit/findAll", "服务器异常！请联系管理员！", model);
            return "mobile_message";
        }
        return "mobile_campus/visitInsert";
    }

    @RequestMapping("/mcampusVisit/save")
    public String save(CampusVisit campusVisit, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        try {
            service.save(campusVisit);
            MessageUtil.returnInfoPage("3", "/mcampusVisit/findAll", "添加成功！", model);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/mcampusVisit/findAll", "服务器异常！请联系管理员！", model);
        }
        return "mobile_message";
    }

    @RequestMapping("/mcampusVisit/updateUI")
    public String updateUI(Model model, Integer campusVisitId,
                           @RequestParam(value = "currentPage") int currentPage,
                           @RequestParam(value = "pageSize") int pageSize) {
        try {
            List<User> users = userService.findMmembers();
            model.addAttribute("users", users);
            CampusVisit campusVisit = service.findById(campusVisitId);
            model.addAttribute("campusVisit", campusVisit);
            model.addAttribute("currentPage", currentPage);
            model.addAttribute("pageSize", pageSize);
            User one = userService.findById(campusVisit.getCampusGemPerson());
            model.addAttribute("one", one);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/mcampusVisit/findAll", "服务器异常！请联系管理员！", model);
        }
        return "mobile_campus/visitUpdate";

    }

    @RequestMapping("/mcampusVisit/update")
    public String update(CampusVisit campusVisit, Model model, HttpSession session,
                         @RequestParam(value = "currentPage") String currentPage,
                         @RequestParam(value = "pageSize", defaultValue = "5") String pageSize) {
        try {
            User user = (User) session.getAttribute("user");
            if (service.update(campusVisit) == 1) {
                MessageUtil.returnInfoPage("3", "/mcampusVisit/findAll", "修改成功！", model);
            } else {
                MessageUtil.returnInfoPage("3", "/mcampusVisit/findAll", "未作修改！", model);
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/campusVisit/findAll?currentPage=" + currentPage + "&pageSize=" + pageSize, "服务器异常！请联系管理员！", model);
        }
        return "mobile_message";
    }


}
